package com.example.mybatis.controller;

import com.example.mybatis.form.UserForm;
import com.example.mybatis.model.PageInfo;
import com.example.mybatis.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.example.mybatis.model.UserTO;
import com.example.mybatis.model.ServiceResponse;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * spring-boot 整合mybatis的 controller
 */
@RestController
@RequestMapping(value="/user")
public class MybatisController {
    @Autowired
    private IUserService userService;


    /**
     * 根据用户账号查询单个用户信息
     * @param userAccout
     * @return
     */
    @RequestMapping(value="/userInfo",method = RequestMethod.GET)
    public ServiceResponse<UserTO> getUserInfoByAccount(@RequestParam("userAccount") String userAccout){

        return userService.queryUserInfoByAccount(userAccout);
    }

    /**
     * 添加单个用户信息,非form表单提交
     * @param userTO
     * @return
     */
    @RequestMapping(value="add/userInfo",method=RequestMethod.POST)
    public ServiceResponse<String> addUserInfo(@RequestBody UserTO userTO) throws Exception{
        //生产环境存储用户的密码等敏感字段是要加密的
        return userService.addUserTO(userTO);
    }

    /**
     * 修改单个用户信息
     * @param userTO
     * @return
     */
    @RequestMapping(value="/update/userInfo",method = RequestMethod.POST)
    public ServiceResponse<String> updateUserInfo(@RequestBody UserTO userTO) throws Exception{

        return userService.updateUserTO(userTO);

    }

    /**
     * 批量添加用户信息
     * @param userTOs
     * @return
     */
    @RequestMapping(value="/batchAdd/userInfo",method=RequestMethod.POST)
    public ServiceResponse<String> batchAddUserInfo(@RequestBody List<UserTO> userTOs)throws Exception{
        return userService.batchAddUserTO(userTOs);

    }

    /**
     * 分页查询用户信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="allUser/page",method=RequestMethod.GET)
    public ServiceResponse<PageInfo> queryAllUser(@RequestParam(value="pageNum") int pageNum,
                                                  @RequestParam(value="pageSize") int pageSize){
        return userService.selectAllUser(pageNum,pageSize);
    }

    /**
     * 批量修改用户信息
     * @param userItems
     * @return
     */
    @RequestMapping(value="/batch/update",method = RequestMethod.POST)
    public ServiceResponse<String> batchUpdateUserInfo(@RequestBody List<UserTO> userItems)throws Exception{

        return userService.batchAddUserTO(userItems);

    }

    /**
     * 根据用户名删除单个用户信息
     * @param userAccount
     * @return
     */
    @RequestMapping(value="/delete/userInfo",method=RequestMethod.DELETE)
    public  ServiceResponse<String> deleteUserInfo(@RequestParam("userAccount") String userAccount)throws Exception{
        return userService.deleteUserInfo(userAccount);
    }

    /**
     * 批量删除用户信息
     * @param userAccounts
     * @return
     */
    @RequestMapping(value="/delete/batch",method = RequestMethod.POST)
    public ServiceResponse<String> batchDeleteUserInfo(@RequestBody List<String> userAccounts)throws Exception{

        return userService.batchDeleteUserInfo(userAccounts);
    }

    /**
     * 用户登录接口，非分布式场景下的单点登录
     * @param formParam
     * @param request
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ServiceResponse<UserTO> userLogin(UserForm formParam, HttpServletRequest request){
        if(StringUtils.isEmpty(formParam.getUserAccount())||StringUtils.isEmpty(formParam.getPassword())){
            throw new IllegalArgumentException("用户账号和密码不能为空!");
        }else{
            ServiceResponse<UserTO> response = userService.userLogin(formParam);
            if(response.getStatus()==200){
                request.getSession().setAttribute("userInfo",response.getData());
            }
            return response;
        }
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/loginOut",method = RequestMethod.GET)
    public ServiceResponse<String> loginOut(HttpServletRequest request){
        ServiceResponse<String> response = new ServiceResponse<>();
        response.setStatus(200);
        HttpSession session = request.getSession();
        if(session.getAttribute("userInfo")!=null){
            session.removeAttribute("userInfo");
            response.setMessage("ok");
            response.setData("用户成功退出登录!");
        }else{
            response.setMessage("failed");
            response.setData("用户尚未登录!");
        }
        return response;
    }

    /**
     * 注册接口
     * @param userTO
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public ServiceResponse<String> userRegister(UserTO userTO)throws Exception{
        checkRegisterParams(userTO);
        return userService.addUserTO(userTO);

    }

    @RequestMapping(value = "/queryUsers",method = RequestMethod.POST)
    public ServiceResponse<List<UserTO>> searchUsers(UserForm userForm)throws Exception{
        return  userService.searchUsers(userForm);
    }



    public void checkRegisterParams(UserTO userTO){
        if(StringUtils.isEmpty(userTO.getUserAccount())){
            throw new IllegalArgumentException("用户账号不能为空!");
        }
        if(StringUtils.isEmpty(userTO.getPassword())){
            throw new IllegalArgumentException("用户密码不能为空!");
        }
        if(StringUtils.isEmpty(userTO.getDeptNo())){
            throw new IllegalArgumentException("用户部门编号不能为空!");
        }
        if(StringUtils.isEmpty(userTO.getPhoneNum())){
            throw new IllegalArgumentException("用户手机号不能为空!");
        }
    }

}
